package hxy.dragon.ktor.router

import hxy.dragon.ktor.module
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.client.statement.bodyAsText
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.*
import io.ktor.server.testing.testApplication
import kotlin.test.Test
import kotlin.test.assertEquals


class UserRouterKtTest {

    @Test
    fun testPostUsers() = testApplication {
        application {
            module()
        }
        client.post("/users") {
            header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
            setBody(
                """
                {
                  "name": "eric",
                  "age": 1
                }
                """.trimIndent()
            )
        }.apply {
            assertEquals(HttpStatusCode.Created, status)
            println("返回值 ${bodyAsText()}")
        }
    }

    @Test
    fun testGetListUsers() = testApplication {
        application {
            module()
        }
        client.get("/list/users").apply {
            assertEquals(HttpStatusCode.OK, status)
        }
    }
}